<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title data-i18n="resources.title_featureSnap"></title>
    <style type="text/css">
        .editPane {
            position: absolute;
            left: 50px;
            top: 10px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_featureSnap"></h5>
    </div>
    <div class='panel-body' id='params'>
        <p></p>
        <div align='right' class='button-group'>
            <input type='button' id='switchSnap' class='btn btn-primary' data-i18n="[value]resources.text_input_value_closeSnap" onclick="switch_snap()"/>
            <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.text_input_value_addData" onclick="addData()"/>
            <input type='button' id='btn3' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear" onclick="clearFeatures()"/>
        </div>
    </div>
</div>
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script type="text/javascript">
    host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var map, layer, vector, modifyFeature, snapState = true, dataAdded = false,
        switchSnap,
        snap01,
        url = host + "/iserver/services/map-world/rest/maps/World";
    //新建矢量图层
    vector = new SuperMap.Layer.Vector("vectorLayer");
    //创建捕捉对象，第一个参数指的是需要进行捕捉的要素图层，后面两个参数分别是点要素和线要素的捕捉容限，第四个参数是附加参数

    snap01 = new SuperMap.Snap([vector], 10, 10, {actived: true});
    //矢量要素编辑控件
    modifyFeature = new SuperMap.Control.ModifyFeature(vector);
    modifyFeature.snap = snap01;

    //定义layer图层，TiledDynamicRESTLayer：分块动态 REST 图层
    layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
        transparent: true,
        cacheEnabled: true
    }, {maxResolution: "auto"});
    switchSnap = document.getElementById("switchSnap");
    //为图层初始化完毕添加addLayer()事件
    layer.events.on({"layerInitialized": addLayer});
    map = new SuperMap.Map("map", {
        controls: [
            new SuperMap.Control.LayerSwitcher(),
            new SuperMap.Control.ScaleLine(),
            new SuperMap.Control.Zoom(),
            new SuperMap.Control.Navigation({
                dragPanOptions: {
                    enableKinetic: true
                }
            }),
            modifyFeature]
    });
    addData();

    function addLayer() {
        map.addLayers([layer, vector]);
        map.setCenter(new SuperMap.LonLat(0, 0), 1);

    }

    function edit_feature() {
        deactiveAll();
        modifyFeature.activate();
        snap01.on();
        snapState = true;
    }

    function deactivate_snap_all() {
        snapState = false;
        snap01.off();
    }

    function activate_snap_all() {
        snapState = true;
        snap01.on();
    }

    function switch_snap() {
        snapState ? switchSnap.value = resources.text_input_value_openSnap : switchSnap.value = resources.text_input_value_closeSnap;
        snapState ? deactivate_snap_all() : activate_snap_all();
    }

    function deactiveAll() {
        modifyFeature.deactivate();
        deactivate_snap_all();
    }

    //移除图层要素
    function clearFeatures() {
        deactiveAll();
        widgets.alert.clearAlert();
        dataAdded = false;
        vector.removeAllFeatures();
    }

    function addData() {
        if (!dataAdded) {
            //点数据
            var point_data = [[-55, 34], [-90, -45], [44, -50], [100, 33], [94, 57]];
            var point_features = [];
            for (var i = 0, len = point_data.length; i < len; i++) {
                var point = new SuperMap.Geometry.Point(point_data[i][0], point_data[i][1]);
                var feature = new SuperMap.Feature.Vector(point);
                point_features.push(feature);
            }
            //线数据
            var line_data = [[113, 19], [107, -2], [92, 13], [90, 21], [82, 12], [74, 3], [64, 22], [52, 8], [71, 0], [91, 3]];
            var points = [];
            for (var i = 0, len = line_data.length; i < len; i++) {
                var point = new SuperMap.Geometry.Point(line_data[i][0], line_data[i][1]);
                points.push(point);
            }
            var line = new SuperMap.Geometry.LineString(points);
            var line_feature = new SuperMap.Feature.Vector(line);
            //面数据
            var polygon_data = [[-16, 30], [-16, 0], [50, 0], [50, 30]];
            var points = [];
            for (var i = 0, len = polygon_data.length; i < len; i++) {
                var point = new SuperMap.Geometry.Point(polygon_data[i][0], polygon_data[i][1]);
                points.push(point);
            }
            var linearRing = new SuperMap.Geometry.LinearRing(points);
            var polygon = new SuperMap.Geometry.Polygon([linearRing]);
            var polygon_feature = new SuperMap.Feature.Vector(polygon);
            point_features.push(line_feature);
            point_features.push(polygon_feature);
            vector.addFeatures(point_features);

            dataAdded = true;
        } else {
            widgets.alert.showAlert(resources.msg_loadedData, true, 220);
        }
        edit_feature();
    }

</script>
</body>
</html>
